package com.wxzhou.wangzhuan.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 广告日志
 */
public class AdLog {
    private Long id;

    //数据上报时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    private LocalDateTime created_at;
    private String deviceid;
    //app渠道，可以为空，分析用
    private String app_channel;
    private Long user_id;
    //广告渠道来源，AdChannel
    private Integer ad_qd;
    //广告网络AdNetwork
    private Integer ad_nt;
    //广告发生时机
    private Integer bs_ac;
    //广告场景
    private Integer ad_sense;
    //广告位id
    private String ad_id;
    //广告id对应的广告位主键
    private Long ad_id_pk;
    //场景唯一值
    private String ad_id_sn;
    private Integer ecpm = 0;
    private BigDecimal real_ad_amount = BigDecimal.ZERO;
    private BigDecimal rate = BigDecimal.ZERO;
    private BigDecimal user_amount = BigDecimal.ZERO;
    private BigDecimal butie_amount = BigDecimal.ZERO;
    private String remark;
    private String ad_info;

    //数据上报时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    private LocalDateTime report_time;
    private Integer ad_type;

    //事务ID
    private String trans_id;


    public AdLog() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getCreated_at() {
        return created_at;
    }

    public void setCreated_at(LocalDateTime created_at) {
        this.created_at = created_at;
    }

    public String getDeviceid() {
        return deviceid;
    }

    public void setDeviceid(String deviceid) {
        this.deviceid = deviceid;
    }

    public Long getUser_id() {
        return user_id;
    }

    public void setUser_id(Long user_id) {
        this.user_id = user_id;
    }

    public Integer getAd_qd() {
        return ad_qd;
    }

    public void setAd_qd(Integer ad_qd) {
        this.ad_qd = ad_qd;
    }

    public Integer getAd_nt() {
        return ad_nt;
    }

    public void setAd_nt(Integer ad_nt) {
        this.ad_nt = ad_nt;
    }

    public Integer getBs_ac() {
        return bs_ac;
    }

    public void setBs_ac(Integer bs_ac) {
        this.bs_ac = bs_ac;
    }

    public String getAd_id() {
        return ad_id;
    }

    public void setAd_id(String ad_id) {
        this.ad_id = ad_id;
    }

    public Long getAd_id_pk() {
        return ad_id_pk;
    }

    public void setAd_id_pk(Long ad_id_pk) {
        this.ad_id_pk = ad_id_pk;
    }

    public String getAd_id_sn() {
        return ad_id_sn;
    }

    public void setAd_id_sn(String ad_id_sn) {
        this.ad_id_sn = ad_id_sn;
    }

    public Integer getEcpm() {
        return ecpm;
    }

    public void setEcpm(Integer ecpm) {
        this.ecpm = ecpm;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    public BigDecimal getUser_amount() {
        return user_amount;
    }

    public void setUser_amount(BigDecimal user_amount) {
        this.user_amount = user_amount;
    }

    public BigDecimal getButie_amount() {
        return butie_amount;
    }

    public void setButie_amount(BigDecimal butie_amount) {
        this.butie_amount = butie_amount;
    }

    public BigDecimal getReal_ad_amount() {
        return real_ad_amount;
    }

    public void setReal_ad_amount(BigDecimal real_ad_amount) {
        this.real_ad_amount = real_ad_amount == null? BigDecimal.ZERO : real_ad_amount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getAd_info() {
        return ad_info;
    }

    public void setAd_info(String ad_info) {
        this.ad_info = ad_info;
    }

    public String getApp_channel() {
        return app_channel;
    }

    public void setApp_channel(String app_channel) {
        this.app_channel = app_channel;
    }

    public LocalDateTime getReport_time() {
        return report_time;
    }

    public void setReport_time(LocalDateTime report_time) {
        this.report_time = report_time;
    }

    public Integer getAd_type() {
        return ad_type;
    }

    public void setAd_type(Integer ad_type) {
        this.ad_type = ad_type;
    }

    public Integer getAd_sense() {
        return ad_sense;
    }

    public void setAd_sense(Integer ad_sense) {
        this.ad_sense = ad_sense;
    }

    public String getTrans_id() {
        return trans_id;
    }

    public void setTrans_id(String trans_id) {
        this.trans_id = trans_id;
    }

    /**
     * 预拉
     * @return Builder实例
     */
    public static Builder buildPreAd(String ad_id, String ad_id_sn, Long user_id, String deviceid, 
                                   Integer ad_qd, Integer ad_nt,  Integer ad_sense) {
        return new Builder(ad_id, ad_id_sn, user_id, deviceid, ad_qd, ad_nt, AdBusinessAction.PRE.getCode(), ad_sense);
    }

    //上报
    public static Builder buildReport(String ad_id, String ad_id_sn, Long user_id, String deviceid,
                                   Integer ad_qd, Integer ad_nt,  Integer ad_sense,Integer bs_ac) {
        return new Builder(ad_id, ad_id_sn, user_id, deviceid, ad_qd, ad_nt, bs_ac, ad_sense);
    }

    /**
     * AdLog的Builder内部类
     */
    public static class Builder {
        private AdLog adLog;

        public Builder(String ad_id, String ad_id_sn, Long user_id, String deviceid, 
                      Integer ad_qd, Integer ad_nt, Integer bs_ac, Integer ad_sense) {
            adLog = new AdLog();
            adLog.setAd_id(ad_id);
            adLog.setAd_id_sn(ad_id_sn);
            adLog.setUser_id(user_id);
            adLog.setDeviceid(deviceid);
            adLog.setAd_qd(ad_qd);
            adLog.setAd_nt(ad_nt);
            adLog.setBs_ac(bs_ac);
            adLog.setAd_sense(ad_sense);
            // 设置创建时间和上报时间为当前时间
            LocalDateTime now = LocalDateTime.now();
            adLog.setCreated_at(now);
            adLog.setReport_time(now);
        }

        /**
         * 设置广告位主键
         * @param ad_id_pk 广告位主键
         * @return Builder实例
         */
        public Builder adIdPk(Long ad_id_pk) {
            adLog.setAd_id_pk(ad_id_pk);
            return this;
        }
        public Builder transId(String transId) {
            adLog.setTrans_id(transId);
            return this;
        }

        /**
         * 设置ecpm
         * @param ecpm ecpm值
         * @return Builder实例
         */
        public Builder ecpm(Integer ecpm) {
            adLog.setEcpm(ecpm);
            return this;
        }

        /**
         * 设置真实广告金额
         * @param real_ad_amount 真实广告金额
         * @return Builder实例
         */
        public Builder realAdAmount(BigDecimal real_ad_amount) {
            adLog.setReal_ad_amount(real_ad_amount);
            return this;
        }

        /**
         * 设置汇率
         * @param rate 汇率
         * @return Builder实例
         */
        public Builder rate(BigDecimal rate) {
            adLog.setRate(rate);
            return this;
        }

        /**
         * 设置用户金额
         * @param user_amount 用户金额
         * @return Builder实例
         */
        public Builder userAmount(BigDecimal user_amount) {
            adLog.setUser_amount(user_amount);
            return this;
        }

        /**
         * 设置补贴金额
         * @param butie_amount 补贴金额
         * @return Builder实例
         */
        public Builder butieAmount(BigDecimal butie_amount) {
            adLog.setButie_amount(butie_amount);
            return this;
        }

        /**
         * 设置备注
         * @param remark 备注
         * @return Builder实例
         */
        public Builder remark(String remark) {
            adLog.setRemark(remark);
            return this;
        }

        /**
         * 设置广告信息
         * @param ad_info 广告信息
         * @return Builder实例
         */
        public Builder adInfo(String ad_info) {
            adLog.setAd_info(ad_info);
            return this;
        }

        /**
         * 设置app渠道
         * @param app_channel app渠道
         * @return Builder实例
         */
        public Builder appChannel(String app_channel) {
            adLog.setApp_channel(app_channel);
            return this;
        }

        /**
         * 设置广告类型
         * @param ad_type 广告类型
         * @return Builder实例
         */
        public Builder adType(Integer ad_type) {
            adLog.setAd_type(ad_type);
            return this;
        }

        public Builder reportTime(long reportTime) {
            Instant instant = Instant.ofEpochSecond(reportTime / 1000, (reportTime % 1000) * 1_000_000);
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            adLog.setReport_time(localDateTime);
            return this;
        }

        /**
         * 构建AdLog实例
         * @return AdLog实例
         */
        public AdLog build() {
            return adLog;
        }
    }
}
